package com.fifth.apartment.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.apartment.entity.*;
import com.fifth.apartment.mapper.EquipmentMapper;
import com.fifth.apartment.service.EquipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fifth.apartment.vo.PageQueryVO;
import com.github.yulichang.query.MPJQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-09
 */
@Service
@Slf4j
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {
    @Autowired
    private EquipmentMapper mapper;

    @Transactional
    @Override
    public IPage<Equipment> findPage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        Page<Equipment> page1 = new Page<>(currentPage,pageSize);
        String[] queryStrings =  vo.getQueryStrings();

        MPJQueryWrapper<Equipment> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(Equipment.class)
                .select("b.name as building","d.room_number as dorm","et.type as type")
                .leftJoin("building_info  b on b.id = t.building_id ")
                .leftJoin("room_info d on d.id = t.room_id")
                .leftJoin("equipment_type et  on et.id = t.type_id")
                .eq(StringUtils.hasLength(queryStrings[0]),"et.id",queryStrings[0])
                .eq(StringUtils.hasLength(queryStrings[1]),"b.id",queryStrings[1])
                .eq(StringUtils.hasLength(queryStrings[2]),"t.state",queryStrings[2]);

//        MPJLambdaWrapper<Equipment> mpjLambdaWrapper = new MPJLambdaWrapper<>();
//        mpjLambdaWrapper.selectAll(Equipment.class)
//                .select(Building::getBuilding)
////               .select(Dorm::getDorm)
////                .select(EquipmentType::getType)
//                .leftJoin(Building.class,Building::getId,Equipment::getBuildingId);
////                .leftJoin(Dorm.class,Dorm::getId,Equipment::getDormId);
////                .leftJoin(EquipmentType.class,EquipmentType::getId,Equipment::getTypeId);
////                .eq(Equipment::getTypeId,string1)
////                .or().eq(Equipment::getBuildingId,string2);



        IPage<Equipment> page2 = mapper.selectJoinPage(page1, Equipment.class,  mpjQueryWrapper);


        return  page2;
    }

    @Override
    public EquipmentType[] findType() {

        EquipmentType[] types = mapper.findType();
        return  types;
    }

    @Override
    public BuildingInfo[] findBuildingType() {
        BuildingInfo[] types = mapper.findBuildingType();
        return types;
    }

    @Override
    public Equipment getEquipmentById(Integer id) {
        Equipment equipment = mapper.selectEquipmentByid(id);
        return equipment;
    }

    @Override
    public Boolean putDetailsById(Integer id, String details, String state) {
        Boolean flag = mapper.updateDetailsById(id,details,state);
        return flag;
    }

    @Override
    public Integer deleteEquipment(Integer id) {
        Integer flag = mapper.deleteById(id);
        return flag;
    }

    @Override
    public Boolean addEquipment(Equipment equipment) {
        int flag = mapper.insert(equipment);
        return flag>0?true:false;
    }


}
